package com.ruoyi.app.business.regiion;

import com.ruoyi.app.converter.RegionConverter;
import com.ruoyi.app.domain.region.RegionResult;
import com.ruoyi.common.core.utils.PageUtils;
import com.ruoyi.common.core.web.page.CommonPageInfo;
import com.ruoyi.service.region.entity.RegionEntity;
import com.ruoyi.service.region.model.RegionModel;
import com.ruoyi.service.region.service.RegionService;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * <p>
 * 作用：注册信息相关服务
 * </p>
 *
 * @author Zane
 * @since 2025-10-03 21:55
 */

@Slf4j
@Component
public class RegionBusiness {

  @Resource
  private RegionService regionService;


  public CommonPageInfo<RegionModel> getProvince() {
    CommonPageInfo<RegionEntity> pageInfo = PageUtils.startPage();
    pageInfo.initData(Optional.ofNullable(regionService.getProvince())
        .orElseGet(ArrayList::new));
    return pageInfo.convert(RegionConverter.INSTANCE::toREgionModel);
  }

  public CommonPageInfo<RegionModel> getCity(Integer provinceId) {
    CommonPageInfo<RegionEntity> pageInfo = PageUtils.startPage();
    pageInfo.initData(Optional.ofNullable(regionService.getCity(provinceId))
        .orElseGet(ArrayList::new));
    return pageInfo.convert(RegionConverter.INSTANCE::toREgionModel);
  }

  public CommonPageInfo<RegionModel> getCounty(Integer cityId) {
    CommonPageInfo<RegionEntity> pageInfo = PageUtils.startPage();
    pageInfo.initData(Optional.ofNullable(regionService.getCounty(cityId))
        .orElseGet(ArrayList::new));
    return pageInfo.convert(RegionConverter.INSTANCE::toREgionModel);
  }

  public List<RegionResult> getCitySplit() {
    final List<RegionModel> cities = Optional.ofNullable(regionService.getAllCities())
        .orElseGet(ArrayList::new).stream().map(RegionConverter.INSTANCE::toREgionModel)
        .collect(Collectors.toList());
    final Map<String, List<RegionModel>> group = cities.stream()
        .collect(Collectors.groupingBy(RegionModel::getPinyinFirst));
    List<RegionResult> results = new ArrayList<>();
    group.keySet().stream().sorted().forEach(key -> {
      final List<RegionModel> models = group.get(key).stream()
          .sorted(Comparator.comparing(RegionModel::getPinyin)).collect(Collectors.toList());
      final RegionResult regionResult = new RegionResult();
      regionResult.setFirstLetter(key);
      regionResult.setRegionModels(models);
      regionResult.setCount(regionResult.getRegionModels().size());
      results.add(regionResult);
    });
    return results;
  }

  public List<RegionEntity> listByIds(List<Integer> ids) {
    return regionService.listByIds(ids);
  }

  public List<RegionEntity> listByIds(Integer[] ids) {
    final List<Integer> idList = Stream.of(ids).collect(Collectors.toList());
    return listByIds(idList);
  }
}
